<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="eptSn">
        <el-input
          v-model="queryParams.eptSn"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="eptName">
        <el-input
          v-model="queryParams.eptName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:eptinfo:add']"
        >新增设备</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:deliver:export']"
        >导出</el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="eptinfoList" border style="width: 100%" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" align="center" prop="number" />
      <el-table-column label="设备状态" align="center" prop="eptState" >
        <template v-slot = "scope">
          <dict-tag :options="dict.type.mes_ept_state" :value="scope.row.eptState"/>
        </template>
      </el-table-column>
      <el-table-column label="设备编号" align="center" prop="eptSn" />
      <el-table-column label="设备名称" align="center" prop="eptName" />
      <el-table-column label="所属部门" align="center" prop="deptId" >
        <template v-slot = "scope">
          <dict-tag :options="dict.type.dept_name" :value="scope.row.deptId"/>
        </template>
      </el-table-column>
      <el-table-column label="设备型号" align="center" prop="eptModel" />
      <el-table-column label="功能描述" align="center" prop="eptUse" />
      <el-table-column label="安装位置" align="center" prop="eptPlace" />
      <el-table-column label="设备负责人" align="center" prop="eptPerson" />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:eptinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:eptinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-form-item label="设备名称" prop="eptName">
          <el-input v-model="form.eptName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备型号" prop="eptModel">
          <el-input v-model="form.eptModel" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="功能描述" prop="eptUse">
          <el-input v-model="form.eptUse" type ="textarea" placeholder="请输入设备功能描述" />
        </el-form-item>
        <el-form-item label="安装位置" prop="eptPlace">
          <el-input v-model="form.eptPlace" placeholder="请输入设备安装位置" />
        </el-form-item>
        <el-form-item label="所属部门" prop="deptId">
          <el-select :disabled="modify" v-model="form.deptId" filterable placeholder="请选择所属部门" style="width:100%">
          <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName" :value="item.deptId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备状态" prop="eptState">
          <el-select v-model="form.eptState" placeholder="请选择设备状态" style="width:100%">
            <el-option label="正常" :value="0"></el-option>
            <el-option label="故障" :value="1"></el-option>
            <el-option label="报废" :value="2"></el-option>
            <el-option label="未启用" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备负责人" prop="eptPerson">
          <el-select :disabled="modify" v-model="form.eptPerson" filterable placeholder="请选择设备负责人" style="width:100%">
            <el-option v-for="item in userOptions" :key="item.userName" :label="item.userName" :value="item.userName"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEptinfo, getEptinfo, delEptinfo, addEptinfo, updateEptinfo } from "@/api/mes/eptinfo";
import {listDept} from "@/api/system/dept";
import {listUser} from "@/api/system/user";

export default {
  name: "Eptinfo",
  dicts:['mes_ept_state','dept_name'],
  data() {
    return {
      //控制下拉框是否可以编辑
      modify: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      //下拉部门信息
      deptOptions:[],
      //设备负责人
      userOptions:[],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备信息表格数据
      eptinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eptSn: null,
        eptName: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        eptName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        eptModel: [
          { required: true, message: "设备型号不能为空", trigger: "blur" }
        ],
        eptUse: [
          { required: true, message: "设备用途不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备信息列表 */
    getList() {
      this.loading = true;
      listEptinfo(this.queryParams).then(response => {
        this.eptinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.calculateIndex()
      });

      //获取部门信息
      listDept().then(response =>{
        this.deptOptions = this.handleTree(response.data);
      })
      //获取设备负责人信息（用户名）
      listUser().then(response =>{
        this.userOptions = response.rows;
        })
    },
    /** 列表编号 */
    calculateIndex() {
      this.eptinfoList.forEach((item, index) => {
        this.$set(item, 'number', index + 1);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        eptSn: null,
        eptName: null,
        eptModel: null,
        eptState: null,
        eptUse: null,
        eptPlace: null,
        eptPerson: null,
        remark: null,
        isDeleted: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增设备信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEptinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEptinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEptinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const eptName = row.eptName || this.eptName;
      this.$modal.confirm('是否确认删除设备名称为"' + eptName + '"的数据项？').then(function() {
        return delEptinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/eptinfo/export', {
        ...this.queryParams
      }, `eptinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
